import { Config, get } from '../components/index.js'
import plugin from '../../../lib/plugins/plugin.js';
import fetch from "node-fetch";
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const fs =require('fs')

const { exec } = require('shelljs');  //需要安装shelljs模块
const botname = global.Bot.nickname;   //bot名字

let cmdcfg = await Config.command
let ban = cmdcfg.forbidden
let head = cmdcfg.head
let path = cmdcfg.path
let separate = cmdcfg.separate
let bottom = cmdcfg.bottom
let notcmds = cmdcfg.notcmds
let no = cmdcfg.no

export class kelitaocan extends plugin {
constructor() {
super({
name: "终端",
dsc: "终端",
event: "message",
priority: 600,
rule: [
{
reg: '^#cmd(.*)$',
fnc: 'runshell'
}
]
})
}

async runshell(e) {
let set = await Config.admin_set
let isplugin = set.cmd
if (!isplugin) return false
if (!e.isMaster) return e.reply(no)
let msg = e.msg.replace("#cmd","");
if (!msg) return e.reply (notcmds)
if (cmdcfg.Blacklist) {
if (ban.some(item => msg.includes(item))) return e.reply('命令违规，原因：该命令在黑名单内') }
exec(msg, {silent: true, cwd: path}, (code, stdout, stderr) => {
if (code !== 0) {//错误信息为常规输出
e.reply("失败！Code: "+code+"\n"+head+msg+"\n"+stderr+separate+"\n"+bottom);
return true
}
e.reply(head+msg+"\n"+stdout+separate+"\n"+bottom);
})
return true;
}
}